﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    class MyBot:MyHttpLoginBase
    {
        string _url = "", _user = "", _pass = "";
        ListBox lb;

        public MyBot(string url, ListBox lb)
        {
            _url = url;
            if (!_url.EndsWith("/"))
                _url += "/";

            this.lb = lb;
        }
        
        public bool Login()
        {
            return Login(_user, _pass);
        }
        
        public bool Login(string user, string pass)
        {
            _user = user;
            _pass = pass;
            string postdata = string.Format("vb_login_username={0}&cookieuser=1&vb_login_password={1}&s=&securitytoken=guest&do=login&vb_login_md5password=&vb_login_md5password_utf=", Encode(user), Encode(pass));
            bool result = SetCookiesForUser(_url + "login.php", postdata);
            return result;
            
        }

        public bool NewThread(string forumid, string topic, string message)
        {
            //string me = _url + "newthread.php?do=newthread&f=" + Encode(forumid);
            //string result = get(me);

            //HTMLForm form = HTMLForm.GetFormByAction(result, "newthread.php?do=postthread&f=" + Encode(forumid));
            //form.Inputs["subject"] = topic;
            //form.Inputs["message"] = message;
            //form.Inputs.Remove("preview");
            //string posta = form.ToPostData();
            //result = post(_url + "newthread.php?do=posthread&f=" + Encode(forumid), posta);
            //return result.Contains("Thank you for posting");
            return false;
        }

        public string NewMessage(string topId, string message)
        {
            string me = _url + "showthread.php?t=" + Encode(topId);
            //string result = _url + "index.php"
            string result = GetContentForCurrentUser(me, lb);

            MyHtmlParser.GetPostsNumberOnPage(result);
            //MyHtmlParser.GetLastPageForTheme(result);
            
            //HTMLForm form = HTMLForm.GetFormByAction(result, "newreply.php?do=postreply&t=" + encode(topId));
            //////form.Inputs["subject"] = topic;
            
            //form.Inputs["message"] = message;
            //form.Inputs.Remove("preview");
            //string posta = form.ToPostData();
            //result = Post(_url + "newreply.php?do=postreply&t=" + encode(topId), posta);
            return result;
            
        }

        static void SaveAsBinaryFormat(object objGraph, string fileName)
        {
            // Сохранить объект в файл CarData.dat в двоичном виде. 
            //BinaryFormatter binFormat = new BinaryFormatter();
            //using (Stream fStream = new FileStream(fileName,
            //FileMode.Create, FileAccess.Write, FileShare.None))
            //{
            //    binFormat.Serialize(fStream, objGraph);
            //}
            //Console.WriteLine("=> Saved car in binary format!");
        }
    }
}
